package com.starrk.myblog.foreground.controller;

import com.alibaba.fastjson.JSON;
import com.starrk.myblog.foreground.entity.Authority;
import com.starrk.myblog.foreground.entity.TbBlogger;
import com.starrk.myblog.foreground.service.MPTbBloggerService;
import com.starrk.myblog.foreground.templete.JedisTemplete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class MainController {
    @Autowired
    private MPTbBloggerService bloggerService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private JedisTemplete jedisTemplete;
    @RequestMapping("/403")
    public String to403(){
        return "403";
    }

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping("/index")
    public String indexx(){
        return "index";
    }

    @RequestMapping("/post")
    public String host(){
        return "post";
    }

    @RequestMapping("/contact")
    public String contact(){
        return "contact";
    }

    @RequestMapping("/about")
    public String about(ModelMap model){
        model.addAttribute("blogger",bloggerService.selectById(1));
        return "about";
    }

    @RequestMapping("/signin")
    public String signin(){
        return "signin";
    }
    @RequestMapping("/regist")
    public String regist(){
        return "regist";
    }


//    @RequestMapping("/test/{prefix}/{key}")
//    @ResponseBody
//    public String  testset(@PathVariable("prefix")String prefix,@PathVariable("key")String key){
//        TbBlogger tb =  bloggerService.selectById(1);
//        String value = JSON.toJSONString(tb);
//        System.out.println(value);
//        jedisTemplete.set(prefix,key,value);
//        return "存储的是"+value;
//    }
//    @RequestMapping("/testget/{prefix}/{key}")
//    @ResponseBody
//    public TbBlogger testget(@PathVariable("prefix")String prefix,@PathVariable("key")String key){
//        String back = jedisTemplete.get(prefix,key);
//        return JSON.parseObject(back,TbBlogger.class);
//    }
    @RequestMapping("/test/sentinel/{key}")
    @ResponseBody
    public TbBlogger testset(@PathVariable("key")String key){

        TbBlogger tb = bloggerService.selectById(1);
        String value = JSON.toJSONString(tb);
        redisTemplate.opsForValue().set(key,value);
        return tb;
    }

    @RequestMapping("/testget/sentinel/{key}")
    @ResponseBody
    public TbBlogger testget(@PathVariable("key")String key){

        String value = (String) redisTemplate.opsForValue().get(key);
        return JSON.parseObject(value,TbBlogger.class);
    }




}
